﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace BbzaApi.Core
{
    /// <summary>
    /// Represents an exchange and its availabiltiy data.
    /// </summary>
    [XmlType("exchange")]
    public class Exchange
    {
        /// <summary>
        /// Gets a value indicating the name of the exchange
        /// </summary>
        [XmlElement("name")]
        public string Name { get; set; }

        /// <summary>
        /// Gets a value indicating the short-code of the exchange
        /// </summary>
        [XmlElement("code")]
        public string Code { get; set; }

        /// <summary>
        /// Gets a value indicating the short-code of the Parent of this exchange.
        /// When the Code of the Exchange matches the ParentCode then this
        /// exchange has no parent.
        /// </summary>
        [XmlElement("parent_code")]
        public string ParentCode { get; set; }

        /// <summary>
        /// Gets a value indicating the type of the parent exchange
        /// </summary>
        /// <remarks>
        /// This value could be DPSU, DSSU.
        /// Please page 14 : https://secure1.telkom.co.za/apps_static/ir/pdf/financial/docs/Technology-DB-2305.pdf
        /// for further details.
        /// </remarks>
        [XmlElement("parent_type")]
        public string ParentType { get; set; }


        /// Gets a value indicating what input was used to match the exchange.
        /// </summary>
        [XmlElement("querymatch")]
        public string QueryMatch { get; set; }

        /// Gets a value indicating 1 if the phone query was an exact match
        /// otherwise 0.
        /// </summary>
        [XmlElement("exact_phone_match")]
        public int ExactPhoneMatch { get; set; }

        /// Gets a value list the exchanges which matched the phone query
        /// </summary>
        [XmlElement("exact_phone_match_list")]
        public string ExactPhoneMatchList { get; set; }

        /// <summary>
        /// Gets a the network availability of this exchange.
        /// </summary>
        [XmlArray("availability")]
        [XmlArrayItem("provider")]
        public Availability Availability { get; set; }

        /// <summary>
        /// Gets a the network availability of this exchange.
        /// </summary>
        [XmlElement("location")]
        public ExchangeLocation Location { get; set; }

        /// <summary>
        /// Gets a value indicating if an error occured.
        /// </summary>
        /// <remarks>
        /// Exchange error codes:
        /// 0 - No error
        /// 1 - Exchange not found
        /// 2 - Exchange location not found (warning)
        /// 3 - Input invalid (phone number)
        /// 4 - Input invalid (exchange code)
        /// 5 - Input invalid (geographic data)
        /// </remarks>
        [XmlElement("errorcode")]
        public int ErrorCode { get; set; }

        /// <summary>
        /// Gets the child exchanges of this exchange.
        /// </summary>
        [XmlArray("child_exchanges")]
        [XmlArrayItem("exchange")]
        public ChildExchanges ChildExchanges { get; set; }


        /// <summary>
        /// Gets the exchanges grouped with this exchange.
        /// </summary>
        [XmlArray("grouped_exchanges")]
        [XmlArrayItem("exchange")]
        public GroupedExchanges GroupedExchanges { get; set; }


        /// <summary>
        /// Gets a human readable explanation of ErrorCode
        /// </summary>
        /// <returns></returns>
        public string GetErrorMessage()
        {
            switch (ErrorCode)
            {
                case 0:
                    return "No error";
                case 1:
                    return "Exchange not found";
                case 2:
                    return "Exchange location not found (warning)";
                case 3:
                    return "Input invalid (phone number)";
                case 4:
                    return "Input invalid (exchange code)";
                case 5:
                    return "Input invalid (geographic code)";
            }
            return "Unknown error code";
        }

        /// <summary>
        /// Gets a value indicating any messages, such as error message.
        /// </summary>
        [XmlElement("message")]
        public string String { get; set; }
    }
}
